package cn.graydove.bot.utils;

import cn.graydove.bot.model.entity.Mock;
import cn.graydove.bot.model.entity.User;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

public final class ExcelUtils {

    public static void genAttackExcel(Collection<User> users, OutputStream os) throws IOException {
        String[] titles = {
                "QQ",
                "用户",
                "出刀次数",
                "总伤害",
        };

        //第一步创建workbook
        HSSFWorkbook wb = new HSSFWorkbook();

        //第二步创建sheet
        HSSFSheet sheet = wb.createSheet("统计信息");
        for (int i = 0; i < titles.length; ++i) {
            sheet.setColumnWidth(i, 15*256);
        }

        //第三步创建行row:添加表头0行
        HSSFRow row = sheet.createRow(0);

        //单元格样式（内容居中）
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);

        //第四步创建单元格
        HSSFCell cell;
        for (int i = 0; i < titles.length; ++i) {
            cell = row.createCell(i);                    //第一个单元格
            cell.setCellValue(titles[i]);                //设定值
            cell.setCellStyle(style);                   //设置样式
        }

        int c = 1;
        for (var u: users) {
            //创建行
            row = sheet.createRow(c++);

            row.createCell(0).setCellValue(u.getQqNumber());
            row.createCell(1).setCellValue(u.getName());
            row.createCell(2).setCellValue(u.getAttack());
            row.createCell(3).setCellValue(u.getDamage());
        }

        wb.write(os);
    }

    public static void genMockExcel(Collection<Mock> mocks, OutputStream os) throws IOException {
        String[] titles = {
                "QQ",
                "用户",
                "周目",
                "Boss",
                "伤害",
        };

        //第一步创建workbook
        HSSFWorkbook wb = new HSSFWorkbook();

        //第二步创建sheet
        HSSFSheet sheet = wb.createSheet("统计信息");
        for (int i = 0; i < titles.length; ++i) {
            sheet.setColumnWidth(i, 15*256);
        }

        //第三步创建行row:添加表头0行
        HSSFRow row = sheet.createRow(0);

        //单元格样式（内容居中）
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);

        //第四步创建单元格
        HSSFCell cell;
        for (int i = 0; i < titles.length; ++i) {
            cell = row.createCell(i);                    //第一个单元格
            cell.setCellValue(titles[i]);                //设定值
            cell.setCellStyle(style);                   //设置样式
        }

        int c = 1;
        for (var m: mocks) {
            //创建行
            row = sheet.createRow(c++);

            row.createCell(0).setCellValue(m.getUserId());
            row.createCell(1).setCellValue(m.getUser());
            row.createCell(2).setCellValue(m.getRound());
            row.createCell(3).setCellValue(m.getBoss());
            row.createCell(4).setCellValue(m.getDamage());
        }

        wb.write(os);
    }
}
